#include <QApplication>
#include <QMainWindow>
#include <QStackedWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <iostream>

int main(int argc, char *argv[])
{
    std::cout << "=== UI TEST PROGRAM ===" << std::endl;
    
    try {
        std::cout << "Creating QApplication..." << std::endl;
        QApplication app(argc, argv);
        std::cout << "QApplication created successfully" << std::endl;
        
        std::cout << "Creating main window..." << std::endl;
        QMainWindow window;
        window.setWindowTitle("UI Test");
        window.resize(400, 300);
        
        std::cout << "Creating central widget..." << std::endl;
        QWidget *centralWidget = new QWidget(&window);
        window.setCentralWidget(centralWidget);
        
        std::cout << "Creating stacked widget..." << std::endl;
        QStackedWidget *stackedWidget = new QStackedWidget(centralWidget);
        
        std::cout << "Creating login page..." << std::endl;
        QWidget *loginPage = new QWidget();
        
        std::cout << "Creating grid layout..." << std::endl;
        QGridLayout *gridLayout = new QGridLayout(loginPage);
        
        std::cout << "Creating UI elements..." << std::endl;
        QLabel *titleLabel = new QLabel("报警系统", loginPage);
        QLabel *accountLabel = new QLabel("账号:", loginPage);
        QLineEdit *accountEdit = new QLineEdit(loginPage);
        QLabel *passwordLabel = new QLabel("密码:", loginPage);
        QLineEdit *passwordEdit = new QLineEdit(loginPage);
        QPushButton *loginButton = new QPushButton("登录", loginPage);
        
        std::cout << "Adding widgets to layout..." << std::endl;
        gridLayout->addWidget(titleLabel, 0, 0, 1, 2);
        gridLayout->addWidget(accountLabel, 1, 0);
        gridLayout->addWidget(accountEdit, 1, 1);
        gridLayout->addWidget(passwordLabel, 2, 0);
        gridLayout->addWidget(passwordEdit, 2, 1);
        gridLayout->addWidget(loginButton, 3, 0, 1, 2);
        
        std::cout << "Setting up stacked widget..." << std::endl;
        stackedWidget->addWidget(loginPage);
        stackedWidget->setCurrentIndex(0);
        
        std::cout << "Setting up main layout..." << std::endl;
        QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
        mainLayout->addWidget(stackedWidget);
        
        std::cout << "Setting default values..." << std::endl;
        accountEdit->setText("admin");
        passwordEdit->setText("1");
        
        std::cout << "Showing window..." << std::endl;
        window.show();
        std::cout << "Window shown successfully" << std::endl;
        
        std::cout << "Starting event loop..." << std::endl;
        return app.exec();
        
    } catch (const std::exception& e) {
        std::cout << "Exception: " << e.what() << std::endl;
        return -1;
    } catch (...) {
        std::cout << "Unknown exception" << std::endl;
        return -2;
    }
}